home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / ViePratique / ArchiFacile / ArchiFacileSetup.exe / {app} / nw.pak / Unnamed File 001160.unknown < prev    next >
Text File  |  2014-10-14  |  15KB  |  499 lines

  1. function Window(routing_id, nobind, predefined_id) {
  2.     // Get and set id.
  3.     native function CallObjectMethod();
  4.     native function CallObjectMethodSync();
  5.     native function AllocateId();
  6.  
  7.     var id;
  8.     if (predefined_id)
  9.         id = predefined_id;
  10.     else
  11.         id = AllocateId();
  12.  
  13.     Object.defineProperty(this, 'id', {
  14.         value: id,
  15.         writable: false
  16.     });
  17.  
  18.     // Store routing id (need for IPC since we are in node's context).
  19.     this.routing_id = routing_id;
  20.  
  21.     // Store myself in node's context.
  22.     global.__nwWindowsStore[id] = this;
  23.     global.__nwObjectsRegistry.set(id, this);
  24.  
  25.     // Tell Shell I'm the js delegate of it.
  26.     native function BindToShell();
  27.     if (!nobind)
  28.         BindToShell(this.routing_id, this.id);
  29.  
  30.     var that = this;
  31.     this.cookies = {
  32.         req_id : 0,
  33.         get : function(details, cb) {
  34.             this.req_id++;
  35.             if (typeof cb == 'function') {
  36.                 that.once('__nw_gotcookie' + this.req_id, function(cookie) {
  37.                     if (cookie.length > 0)
  38.                         cb(cookie[0]);
  39.                     else
  40.                         cb(null);
  41.                 });
  42.             }
  43.             CallObjectMethod(that, 'CookieGet', [ this.req_id, details ]);
  44.         },
  45.         getAll : function(details, cb) {
  46.             this.req_id++;
  47.             if (typeof cb == 'function') {
  48.                 that.once('__nw_gotcookie' + this.req_id, function(cookie) {
  49.                     cb(cookie);
  50.                 });
  51.             }
  52.             CallObjectMethod(that, 'CookieGetAll', [ this.req_id, details ]);
  53.         },
  54.         remove : function(details, cb) {
  55.             this.req_id++;
  56.             if (typeof cb == 'function') {
  57.                 that.once('__nw_gotcookie' + this.req_id, function(details) {
  58.                     cb(details);
  59.                 });
  60.             }
  61.             CallObjectMethod(that, 'CookieRemove', [ this.req_id, details ]);
  62.         },
  63.         set : function(details, cb) {
  64.             this.req_id++;
  65.             if (typeof cb == 'function') {
  66.                 that.once('__nw_gotcookie' + this.req_id, function(cookie) {
  67.                     cb(cookie);
  68.                 });
  69.             }
  70.             CallObjectMethod(that, 'CookieSet', [ this.req_id, details ]);
  71.         },
  72.         onChanged : {
  73.             addListener : function(cb) {
  74.                 that.on('__nw_cookie_changed', cb);
  75.             },
  76.             removeListener : function(cb) {
  77.                 that.removeListener('__nw_cookie_changed', cb);
  78.             }
  79.         }
  80.     }
  81. }
  82.  
  83. // Window will inherit EventEmitter in "third_party/node/src/node.js", do
  84. // not inherit here becuase this file is loaded before everything else.
  85.  
  86. // And init everything after the inheritance.
  87. Window.init = function() {
  88.  
  89. var v8_util = process.binding('v8_util');
  90. var EventEmitter = process.EventEmitter;
  91.  
  92. native function CallObjectMethod();
  93. native function CallObjectMethodSync();
  94.  
  95. // Override the addListener method.
  96. Window.prototype.on = Window.prototype.addListener = function(ev, callback) {
  97.   // Save window id of where the callback is created.
  98.   var closure = v8_util.getCreationContext(callback);
  99.   if (v8_util.getConstructorName(closure) == 'Window' &&
  100.       closure.hasOwnProperty('nwDispatcher')) {
  101.     v8_util.setHiddenValue(callback, '__nwWindowId',
  102.         closure.nwDispatcher.requireNwGui().Window.get().id);
  103.   }
  104.  
  105.   // Call parent.
  106.   EventEmitter.prototype.addListener.apply(this, arguments);
  107. }
  108.  
  109. // Route events.
  110. Window.prototype.handleEvent = function(ev) {
  111.   // Filter invalid callbacks.
  112.   var listeners_copy = this.listeners(ev).slice(0);
  113.   for (var i = 0; i < listeners_copy.length; ++i) {
  114.     var original_closure = v8_util.getCreationContext(listeners_copy[i]);
  115.  
  116.     // Skip for node context.
  117.     if (v8_util.getConstructorName(original_closure) != 'Window')
  118.       continue;
  119.  
  120.  
  121.     var window_id = v8_util.getHiddenValue(listeners_copy[i], '__nwWindowId');
  122.  
  123.     // Remove callback if original window is closed (not in __nwWindowsStore).
  124.     if (global.__nwWindowsStore.hasOwnProperty(window_id)) {
  125.       // Check hashes and see if the window context of Shell has been changed,
  126.       // this happens when we refresh the Shell or change it's locations.
  127.       var original_hash = v8_util.getObjectHash(original_closure);
  128.       var current_hash = v8_util.getObjectHash(
  129.           global.__nwWindowsStore[window_id].window);
  130.  
  131.       // Do nothing if nothing is changed.
  132.       if (original_hash == current_hash)
  133.         continue;
  134.       if (original_closure.window.top === global.__nwWindowsStore[window_id].window) //iframe case
  135.         continue;
  136.     }
  137.  
  138.     console.warn('Remove zombie callback for window id ' + window_id + " ev: " + ev);
  139.     this.removeListener(ev, listeners_copy[i]);
  140.   }
  141.  
  142.     if (ev == 'new-win-policy' && arguments.length > 3) {
  143.         var policy = arguments[3];
  144.         policy.ignore         =  function () { this.val = 'ignore'; };
  145.         policy.forceCurrent   =  function () { this.val = 'current'; };
  146.         policy.forceDownload  =  function () { this.val = 'download'; };
  147.         policy.forceNewWindow =  function () { this.val = 'new-window'; };
  148.         policy.forceNewPopup  =  function () { this.val = 'new-popup'; };
  149.     }
  150.   // Route events to EventEmitter.
  151.   this.emit.apply(this, arguments);
  152.  
  153.   // If no one is listening to 'close' then close directly
  154.   if (ev == 'close' && this.listeners(ev).length == 0) {
  155.     this.close(true);
  156.     return;
  157.   } else if (ev == 'closed') { // Clear me.
  158.     delete global.__nwWindowsStore[this.id];
  159.     return;
  160.   }
  161. }
  162.  
  163. // Return current window object of Shell's DOM.
  164. Window.prototype.__defineGetter__('window', function() {
  165.   native function GetWindowObject();
  166.   return GetWindowObject(this.routing_id);
  167. });
  168.  
  169. Window.prototype.__defineSetter__('x', function(x) {
  170.   this.moveTo(x, this.y);
  171. });
  172.  
  173. Window.prototype.__defineGetter__('x', function() {
  174.   return CallObjectMethodSync(this, 'GetPosition', [])[0];
  175. });
  176.  
  177. Window.prototype.__defineSetter__('y', function(y) {
  178.   this.moveTo(this.x, y);
  179. });
  180.  
  181. Window.prototype.__defineGetter__('y', function() {
  182.   return CallObjectMethodSync(this, 'GetPosition', [])[1];
  183. });
  184.  
  185. Window.prototype.__defineSetter__('width', function(width) {
  186.   this.resizeTo(width, this.height);
  187. });
  188.  
  189. Window.prototype.__defineGetter__('width', function() {
  190.   return CallObjectMethodSync(this, 'GetSize', [])[0];
  191. });
  192.  
  193. Window.prototype.__defineSetter__('height', function(height) {
  194.   this.resizeTo(this.width, height);
  195. });
  196.  
  197. Window.prototype.__defineGetter__('height', function() {
  198.   return CallObjectMethodSync(this, 'GetSize', [])[1];
  199. });
  200.  
  201. Window.prototype.__defineSetter__('title', function(title) {
  202.   this.window.document.title = title;
  203. });
  204.  
  205. Window.prototype.__defineGetter__('title', function() {
  206.   return this.window.document.title;
  207. });
  208.  
  209. Window.prototype.__defineSetter__('zoomLevel', function(level) {
  210.   CallObjectMethodSync(this, 'SetZoomLevel', level);
  211. });
  212.  
  213. Window.prototype.__defineGetter__('zoomLevel', function() {
  214.   return CallObjectMethodSync(this, 'GetZoomLevel', [])[0];
  215. });
  216.  
  217. Window.prototype.__defineSetter__('menu', function(menu) {
  218.   if(!menu) {
  219.     CallObjectMethod(this, "ClearMenu", []);
  220.     return;
  221.   }
  222.   if (v8_util.getConstructorName(menu) != 'Menu')
  223.     throw new String("'menu' property requries a valid Menu");
  224.  
  225.   if (menu.type != 'menubar')
  226.     throw new String('Only menu of type "menubar" can be used as this.window menu');
  227.  
  228.   v8_util.setHiddenValue(this, 'menu', menu);
  229.   CallObjectMethod(this, 'SetMenu', [ menu.id ]);
  230. });
  231.  
  232. Window.prototype.__defineGetter__('menu', function() {
  233.   return v8_util.getHiddenValue(this, 'menu');
  234. });
  235.  
  236. Window.prototype.__defineSetter__('isFullscreen', function(flag) {
  237.   if (flag)
  238.     this.enterFullscreen();
  239.   else
  240.     this.leaveFullscreen();
  241. });
  242.  
  243. Window.prototype.isDevToolsOpen = function () {
  244.     var result = CallObjectMethodSync(this, 'IsDevToolsOpen', []);
  245.     return Boolean(result[0]);
  246. }
  247.  
  248. Window.prototype.__defineGetter__('isFullscreen', function() {
  249.   var result = CallObjectMethodSync(this, 'IsFullscreen', []);
  250.   return Boolean(result[0]);
  251. });
  252.  
  253. Window.prototype.__defineSetter__('isKioskMode', function(flag) {
  254.   if (flag)
  255.     this.enterKioskMode();
  256.   else
  257.     this.leaveKioskMode();
  258. });
  259.  
  260. Window.prototype.__defineGetter__('isKioskMode', function() {
  261.   var result = CallObjectMethodSync(this, 'IsKioskMode', []);
  262.   return Boolean(result[0]);
  263. });
  264.  
  265. Window.prototype.moveTo = function(x, y) {
  266.   CallObjectMethod(this, 'MoveTo', [ Number(x), Number(y) ]);
  267. }
  268.  
  269. Window.prototype.moveBy = function(x, y) {
  270.   var position = CallObjectMethodSync(this, 'GetPosition', []);
  271.   this.moveTo(position[0] + x, position[1] + y);
  272. }
  273.  
  274. Window.prototype.resizeTo = function(width, height) {
  275.   CallObjectMethod(this, 'ResizeTo', [ Number(width), Number(height) ]);
  276. }
  277.  
  278. Window.prototype.resizeBy = function(width, height) {
  279.   var size = CallObjectMethodSync(this, 'GetSize', []);
  280.   this.resizeTo(size[0] + width, size[1] + height);
  281. }
  282.  
  283. Window.prototype.focus = function(flag) {
  284.   if (typeof flag == 'undefined' || Boolean(flag)) {
  285.       if (this.__nw_is_devtools)
  286.           CallObjectMethod(this, 'Focus', []);
  287.       else
  288.           this.window.focus();
  289.   } else
  290.     this.blur();
  291. };
  292.  
  293. Window.prototype.blur = function() {
  294.     if (this.__nw_is_devtools)
  295.         CallObjectMethod(this, 'Blur', []);
  296.     else
  297.         this.window.blur();
  298. };
  299.  
  300. Window.prototype.show = function(flag) {
  301.   if (typeof flag == 'undefined' || Boolean(flag))
  302.     CallObjectMethod(this, 'Show', []);
  303.   else
  304.     this.hide();
  305. }
  306.  
  307. Window.prototype.hide = function() {
  308.   CallObjectMethod(this, 'Hide', []);
  309. }
  310.  
  311. Window.prototype.close = function(force) {
  312.   CallObjectMethod(this, 'Close', [ Boolean(force) ]);
  313. }
  314.  
  315. Window.prototype.maximize = function() {
  316.   CallObjectMethod(this, 'Maximize', []);
  317. }
  318.  
  319. Window.prototype.unmaximize = function() {
  320.   CallObjectMethod(this, 'Unmaximize', []);
  321. }
  322.  
  323. Window.prototype.minimize = function() {
  324.   CallObjectMethod(this, 'Minimize', []);
  325. }
  326.  
  327. Window.prototype.restore = function() {
  328.   CallObjectMethod(this, 'Restore', []);
  329. }
  330.  
  331. Window.prototype.enterFullscreen = function() {
  332.   CallObjectMethod(this, 'EnterFullscreen', []);
  333. }
  334.  
  335. Window.prototype.leaveFullscreen = function() {
  336.   CallObjectMethod(this, 'LeaveFullscreen', []);
  337. }
  338.  
  339. Window.prototype.toggleFullscreen = function() {
  340.   CallObjectMethod(this, 'ToggleFullscreen', []);
  341. }
  342.  
  343. Window.prototype.enterKioskMode = function() {
  344.   CallObjectMethod(this, 'EnterKioskMode', []);
  345. }
  346.  
  347. Window.prototype.leaveKioskMode = function() {
  348.   CallObjectMethod(this, 'LeaveKioskMode', []);
  349. }
  350.  
  351. Window.prototype.toggleKioskMode = function() {
  352.   CallObjectMethod(this, 'ToggleKioskMode', []);
  353. }
  354.  
  355. Window.prototype.closeDevTools = function() {
  356.   CallObjectMethod(this, 'CloseDevTools', []);
  357. }
  358.  
  359. Window.prototype.showDevTools = function(frm, headless) {
  360.     var id = '';
  361.     if (typeof frm === 'string') {
  362.         id = frm;
  363.         this._pending_devtools_jail = null;
  364.     }else{
  365.         this._pending_devtools_jail = frm;
  366.     }
  367.     var win_id = CallObjectMethodSync(this, 'ShowDevTools', [id, Boolean(headless)])[0];
  368.     var ret;
  369.     if (typeof win_id == 'number' && win_id > 0) {
  370.         ret = global.__nwWindowsStore[win_id];
  371.         if (!ret) {
  372.             ret = new global.Window(this.window.nwDispatcher.getRoutingIDForCurrentContext(), true, win_id);
  373.             ret.__nw_is_devtools = true;
  374.         }
  375.         return ret;
  376.     }
  377. }
  378.  
  379. Window.prototype.__setDevToolsJail = function(id) {
  380.     var frm = null;
  381.     if (id)
  382.         frm = this.window.document.getElementById(id);
  383.     else
  384.         frm = this._pending_devtools_jail || null;
  385.     CallObjectMethod(this, 'setDevToolsJail', frm);
  386. }
  387.  
  388. Window.prototype.setMinimumSize = function(width, height) {
  389.   CallObjectMethod(this, 'SetMinimumSize', [ width, height ]);
  390. }
  391.  
  392. Window.prototype.setMaximumSize = function(width, height) {
  393.   CallObjectMethod(this, 'SetMaximumSize', [ width, height ]);
  394. }
  395.  
  396. Window.prototype.setResizable = function(resizable) {
  397.   resizable = Boolean(resizable);
  398.   CallObjectMethod(this, 'SetResizable', [ resizable ]);
  399. }
  400.  
  401. Window.prototype.setAlwaysOnTop = function(flag) {
  402.   CallObjectMethod(this, 'SetAlwaysOnTop', [ Boolean(flag) ]);
  403. }
  404.  
  405. Window.prototype.setShowInTaskbar = function(flag) {
  406.   flag = Boolean(flag);
  407.   CallObjectMethod(this, 'SetShowInTaskbar', [ flag ]);
  408. }
  409.  
  410. Window.prototype.requestAttention = function(flash) {
  411.   if (typeof flash == 'boolean') {
  412.     // boolean true is redirected as -1 value
  413.     flash = flash ? -1 : 0;
  414.   }
  415.   CallObjectMethod(this, 'RequestAttention', [ flash ]);
  416. }
  417.  
  418. Window.prototype.setBadgeLabel = function(label) {
  419.   label = "" + label;
  420.   CallObjectMethod(this, 'SetBadgeLabel', [ label ]);
  421. }
  422.  
  423. Window.prototype.setProgressBar = function(progress) {
  424.   if (typeof progress != "number")
  425.     throw new String('progress must be a number');
  426.   CallObjectMethod(this, 'SetProgressBar', [ progress ]);
  427. }
  428.  
  429. Window.prototype.setPosition = function(position) {
  430.   if (position != 'center' && position != 'mouse')
  431.     throw new String('Invalid postion');
  432.   CallObjectMethod(this, 'SetPosition', [ position ]);
  433. }
  434.  
  435. Window.prototype.reload = function(type) {
  436.   // type is default to 0 (RELOAD).
  437.   if (!(typeof type == 'number' && 0 <= type && type <= 3))
  438.     type = 0;
  439.  
  440.   CallObjectMethod(this, 'Reload', [ type ]);
  441. }
  442.  
  443. Window.prototype.reloadIgnoringCache = function() {
  444.   this.reload(1);
  445. }
  446.  
  447. Window.prototype.reloadOriginalRequestURL = function() {
  448.   this.reload(2);
  449. }
  450.  
  451. Window.prototype.reloadDev = function() {
  452.   this.reload(3);
  453. }
  454.  
  455. var mime_types = {
  456.   'jpeg' : 'image/jpeg',
  457.   'png'  : 'image/png'
  458. }
  459.  
  460. Window.prototype.capturePage = function(callback, image_format_options) {
  461.   var options;
  462.  
  463.   // Be compatible with the old api capturePage(callback, [format string])
  464.   if (typeof image_format_options == 'string' || image_format_options instanceof String) {
  465.     options = {
  466.       format : image_format_options
  467.     };
  468.   } else {
  469.     options = image_format_options || {};
  470.   }
  471.  
  472.   if (options.format != 'jpeg' && options.format != 'png') {
  473.     options.format = 'jpeg';
  474.   }
  475.  
  476.   if (typeof callback == 'function') {
  477.     this.once('__nw_capturepagedone', function(imgdata) {
  478.       switch(options.datatype){
  479.         case 'buffer' :
  480.           callback(new Buffer(imgdata, "base64"));
  481.           break;
  482.         case 'raw' :
  483.           callback(imgdata);
  484.         case 'datauri' :
  485.         default :
  486.           callback('data:' + mime_types[options.format] + ';base64,' + imgdata );
  487.       }
  488.     });
  489.   }
  490.  
  491.   CallObjectMethod(this, 'CapturePage', [options.format]);
  492. };
  493.  
  494.     Window.prototype.eval = function(frame, script) {
  495.         return CallObjectMethod(this, 'EvaluateScript', frame, script);
  496.     };
  497.  
  498. }  // function Window.init
  499.